package com.blacktech.dbu.core.dto;

import java.util.List;
import java.util.Map;

/**
 * 通用搜索请求DTO
 * 提供统一的搜索、过滤、排序和分页参数
 *
 * @author Yanyunsong
 */
public class SearchRequest {

    /**
     * 搜索关键词（在指定的字段中进行模糊搜索）
     */
    private String keyword;

    /**
     * 精确匹配条件
     */
    private Map<String, Object> exactMatches;

    /**
     * 模糊匹配条件
     */
    private Map<String, String> fuzzyMatches;

    /**
     * 范围查询条件
     */
    private Map<String, RangeValue> rangeValues;

    /**
     * 排序字段列表
     */
    private List<SortField> sortFields;

    /**
     * 分页参数
     */
    private Pagination pagination;

    /**
     * 空构造器
     */
    public SearchRequest() {
    }

    /**
     * 构造器
     */
    public SearchRequest(String keyword) {
        this.keyword = keyword;
    }

    /**
     * 范围值内部类
     */
    public static class RangeValue {
        private Object min;
        private Object max;
        private boolean includeMin = true;
        private boolean includeMax = true;

        public RangeValue() {
        }

        public RangeValue(Object min, Object max) {
            this.min = min;
            this.max = max;
        }

        public RangeValue(Object min, Object max, boolean includeMin, boolean includeMax) {
            this.min = min;
            this.max = max;
            this.includeMin = includeMin;
            this.includeMax = includeMax;
        }

        // Getters and Setters
        public Object getMin() {
            return min;
        }

        public void setMin(Object min) {
            this.min = min;
        }

        public Object getMax() {
            return max;
        }

        public void setMax(Object max) {
            this.max = max;
        }

        public boolean isIncludeMin() {
            return includeMin;
        }

        public void setIncludeMin(boolean includeMin) {
            this.includeMin = includeMin;
        }

        public boolean isIncludeMax() {
            return includeMax;
        }

        public void setIncludeMax(boolean includeMax) {
            this.includeMax = includeMax;
        }
    }

    /**
     * 排序字段内部类
     */
    public static class SortField {
        private String field;
        private String direction = "asc"; // asc 或 desc

        public SortField() {
        }

        public SortField(String field) {
            this.field = field;
            this.direction = "asc";
        }

        public SortField(String field, String direction) {
            this.field = field;
            this.direction = direction;
        }

        // Getters and Setters
        public String getField() {
            return field;
        }

        public void setField(String field) {
            this.field = field;
        }

        public String getDirection() {
            return direction;
        }

        public void setDirection(String direction) {
            this.direction = direction;
        }
    }

    /**
     * 分页参数内部类
     */
    public static class Pagination {
        private int page = 0;        // 页码，从0开始
        private int size = 20;       // 每页大小
        private boolean enabled = true; // 是否启用分页

        public Pagination() {
        }

        public Pagination(int page, int size) {
            this.page = page;
            this.size = size;
        }

        public Pagination(int page, int size, boolean enabled) {
            this.page = page;
            this.size = size;
            this.enabled = enabled;
        }

        // Getters and Setters
        public int getPage() {
            return page;
        }

        public void setPage(int page) {
            this.page = page;
        }

        public int getSize() {
            return size;
        }

        public void setSize(int size) {
            this.size = size;
        }

        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }
    }

    // Getters and Setters
    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public Map<String, Object> getExactMatches() {
        return exactMatches;
    }

    public void setExactMatches(Map<String, Object> exactMatches) {
        this.exactMatches = exactMatches;
    }

    public Map<String, String> getFuzzyMatches() {
        return fuzzyMatches;
    }

    public void setFuzzyMatches(Map<String, String> fuzzyMatches) {
        this.fuzzyMatches = fuzzyMatches;
    }

    public Map<String, RangeValue> getRangeValues() {
        return rangeValues;
    }

    public void setRangeValues(Map<String, RangeValue> rangeValues) {
        this.rangeValues = rangeValues;
    }

    public List<SortField> getSortFields() {
        return sortFields;
    }

    public void setSortFields(List<SortField> sortFields) {
        this.sortFields = sortFields;
    }

    public Pagination getPagination() {
        return pagination;
    }

    public void setPagination(Pagination pagination) {
        this.pagination = pagination;
    }

    /**
     * 判断是否启用分页
     */
    public boolean isPaginationEnabled() {
        return pagination != null && pagination.isEnabled();
    }

    /**
     * 获取页码
     */
    public int getPage() {
        return pagination != null ? pagination.getPage() : 0;
    }

    /**
     * 获取每页大小
     */
    public int getSize() {
        return pagination != null ? pagination.getSize() : 20;
    }
}